package assignment1.inheritance;

public class Test {

	public static void main(String[] args) {

		////////////////////////////////////////////////////////////
		// Test for one instance, one class, one function
		////////////////////////////////////////////////////////////
		Instance myInst = new Instance();
		myInst.instClass = new Class("superclass");
		myInst.instClass.dict.put("func1", new Function() { // add new function to class
					@Override
					public void execute() {
						System.out.println("test invoked");
					}
		});
		invoke(myInst, "func1");
		System.out.println("\n");
		
		////////////////////////////////////////////////////////////
		// Test for three instances, three classes, one function
		////////////////////////////////////////////////////////////		
		Class subclass = myInst.instClass.inherit("subclass");
		Instance myInst2 = new Instance();
		myInst2.instClass = subclass;
		Class subsubclass = myInst2.instClass.inherit("subsubclass");
		Instance myInst3 = new Instance();
		myInst3.instClass = subsubclass;
		
		invoke(myInst3, "func1");
		System.out.println("\n");
		
		myInst2.instClass.dict.put("func2", new Function() { // add new function to class
					@Override
					public void execute() {
						System.out.println("test invoked");
					}
		});	
		
		////////////////////////////////////////////////////////////
		// Test for three instances, new function in subclass
		////////////////////////////////////////////////////////////		
		invoke(myInst3, "func2");
		invoke(myInst, "func2");
	}
	
		
	
	// lookup algorithm for methods
	public static void invoke(Instance obj, String func) {
		Function myFunc = obj.instClass.searchFunc(func);
		if (myFunc != null) {
			myFunc.execute();
			System.out.println(func);
		} else {
			System.out.println("ERROR");
		}
	}
}
